package com.ftwj.demo.base.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
*
* <p>Title: StringUtil</p>  
* <p>Description: SQL语句构造使用到字符串工具</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSLi
* @date 2020年3月22日 上午12:12:45
* @version V1.0
 */
public class StringUtil {
	
	/**
     *	驼峰法则的Mapper <br/>
	 * 	如：数据库字段my_teacher_name 转成 myTeacherName
     */
	public static Map<String, Object> rowMapper2Hump(ResultSet rs) throws SQLException{
		StringBuilder camelKey = new StringBuilder();
		Map<String,Object> camelKeyMap = new HashMap<String,Object>();
		int column = rs.getMetaData().getColumnCount();
		for(int i=1;i<=column;i++){
			camelKey.delete(0, camelKey.length());
			String columnLabel = rs.getMetaData().getColumnLabel(i);
			if(!columnLabel.contains("_")){
				camelKeyMap.put(columnLabel, rs.getObject(i));
				continue;
			}
			for(int j=0,len = columnLabel.length();j<len;j++){
				char c = columnLabel.charAt(j);
				if(c != '_'){
					camelKey.append(c);
					continue;	
				}
				if(++j < len) 
					camelKey.append(Character.toUpperCase(columnLabel.charAt(j)));
			}
			camelKeyMap.put(camelKey.toString(), rs.getObject(i));
		}
		return camelKeyMap;
	}
	
}
